<?php

namespace backend\modules\shop\controllers;

use common\components\Upload;
use Yii;
use common\models\shop\ShopGoods;
use backend\modules\shop\models\ShopGoodsSearch;
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * GoodsController implements the CRUD actions for ShopGoods model.
 */
class GoodsController extends Controller
{


    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'upload' => [
                'class' => 'kucha\ueditor\UEditorAction',
                'config' => [
                    "imageUrlPrefix"  => Yii::getAlias('@uploadurl'),//图片访问路径前缀
                    "pathFormat" => "/images/{yyyy}/{mm}/{rand:7}" //上传保存路径
                ],
            ]
        ];
    }

    /**
     * Lists all ShopGoods models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ShopGoodsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ShopGoods model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ShopGoods model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ShopGoods();

        if ($model->load(Yii::$app->request->post())) {
            if(false !== $photo = Upload::upload($model,'thumb')){
                $model->setAttribute('thumb',$photo);
            }
            if(false !== $photo = Upload::upload($model,'mutithumb')){
                $model->setAttribute('mutithumb',$photo);
            }
            $model->timestart = strtotime($model->timestart);
            $model->timeend = strtotime($model->timeend);
            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('tabx', [
                    'model' => $model,
                    'op' => 'create',
                ]);
            }
        }  else {
            return $this->render('tabx', [
                'model' => $model,
                'op' => 'create',
            ]);
        }
    }

    /**
     * Updates an existing ShopGoods model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $model->setScenario('update');
            if(false !== $photo = Upload::upload($model,'thumb')){
                $model->setAttribute('thumb',$photo);
            }
            if(false !== $photo = Upload::upload($model,'mutithumb')){
                $model->setAttribute('mutithumb',$photo);
            }
            $model->timestart = strtotime($model->timestart);
            $model->timeend = strtotime($model->timeend);
            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                $model->timestart = date('Y-m-d H:i:s',$model->timestart);
                $model->timeend = date('Y-m-d H:i:s',$model->timeend);
                return $this->render('tabx', [
                    'model' => $model,
                    'op' => 'update',
                ]);
            }
        }  else {
            $model->timestart = date('Y-m-d H:i:s',$model->timestart);
            $model->timeend = date('Y-m-d H:i:s',$model->timeend);
            return $this->render('tabx', [
                'model' => $model,
                'op' => 'update',
            ]);
        }
    }

    /**
     * Deletes an existing ShopGoods model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }



    /**
     * Finds the ShopGoods model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return ShopGoods the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ShopGoods::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }




    //添加规格模板
    public function actionSpec()
    {
        $this->layout = false;

        var_dump(Yii::$app->request->post());
        exit;

        $title = Yii::$app->request->get('title');
        $spec = [
            "id" => rand(100,999).time(),
            "title" => $title
        ];
        return $this->render('_spec',['spec'=>$spec]);
    }


    //添加规格项模板
    public function actionSpecitem()
    {
        $title = Yii::$app->request->get('title');


        $spec = array(
            "id" => Yii::$app->request->get('specid')
        );

        $specitem = [
            "id" => rand(1000,9999).time(),
            "title" => $title,
            "show" => 1
        ];
        return $this->renderPartial('_spec_item',['specitem'=>$specitem,'spec'=>$spec]);

    }
}
